#include "threadpool.h"
#include <iostream>

using ulong = unsigned long long;

class MyTask :public Task
{
public:
	MyTask(ulong begin,ulong end)
		:begin_(begin)
		,end_(end)
	{
	}
	~MyTask()
	{
	}
	Any run()
	{
		std::cout << "task run begin using pid=" << std::this_thread::get_id() << "  taskID="<<taskId_<<std::endl;
		ulong sum = 0;
		for (ulong i = begin_; i <= end_; i++)
		{
			sum += i;
		}
		std::this_thread::sleep_for(std::chrono::seconds(3));
		std::cout << "task run end tid=" << std::this_thread::get_id() << "  taskID=" << taskId_ << std::endl;
		return sum;
	}
private:
	ulong begin_;
	ulong end_;
};


int main(int argc, char** argv)
{
	ThreadPool pool;
	//pool.setMode(PoolMode::MODE_CACHED);
	pool.start(4);
		Result res1 = pool.submitTask(std::make_shared<MyTask>(1, 100000000));
		std::this_thread::sleep_for(std::chrono::seconds(1));
		Result res2 = pool.submitTask(std::make_shared<MyTask>(100000001, 200000000));
		std::this_thread::sleep_for(std::chrono::seconds(1));
		Result res3 = pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		std::this_thread::sleep_for(std::chrono::seconds(1));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		std::this_thread::sleep_for(std::chrono::seconds(1));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
		std::this_thread::sleep_for(std::chrono::seconds(1));
		pool.submitTask(std::make_shared<MyTask>(200000001, 300000000));
	ulong sum = res1.get().cast<ulong>() + res2.get().cast<ulong>() + res3.get().cast<ulong>();
	std::cout << "get sum" << std::endl;
	std::cout << sum << std::endl;
	getchar();


	return 0;
}